Skip to main content

put cookie

Type

command

Summary

Sets a cookie in a visiting browser for a given URL.

Syntax

put [secure] [httponly] cookie <cookieName> [for <filePath>] [on <cookieDomain>] with <cookieValue> [until <expiryTime>]

Description

Use the put cookie command to create a cookie with the given data in the visiting browser for a specific URL.

If the secure form is used, the browser only sends the cookie if communicating over a secure connection (https).

If the httponly form is used, the cookie is only available from the browser when communicating with the server and hidden from client-side scripting (for example JavaScript).

A cookie is used by a website to store state information in the user's browser, which is returned to the website on subsequent visits. Cookies are often used to store user preferences and session data. For example, you may allow a user to choose a theme through which to view your website and store this choice in a cookie to be used on subsequent visits.

When setting a cookie you can specify a path. Cookies are returned to all URLs within the specified path. For example, a cookie set on the domain "www.livecode.com" with the following path:

    /products/

is available to all pages within and under http://www.livecode.com/products/

To make the cookie available across your entire website, set the path to "/".

note

Cookies must be set before any headers are sent.

Parameters

NameTypeDescription

cookieName

string

A string containing the name by which the cookie is identified.

filePath

string

A string containing the path on the server at which the cookie is available. If a path is not specified, then the containing folder of the current URL is used.

cookieDomain

string

A string containing the domain at which the cookie is available. If the domain is not specified, then the domain of the current URL is used.

cookieValue

string

A string containing the contents of the cookie. This is automatically URL encoded.

expiryTime

A number containing the time at which the cookie expires. This is expressed in seconds since the UNIX epoch (midnight, January 1, 1970 GMT). If this is not specified or empty, the cookie expires when the client browser closes.

Examples

put cookie "testcookie1" with "some cookie value"
-- Expires in 1 year
put cookie "testcookie2" with "some cookie value" until (the seconds + 60 * 60 * 24 * 365)
-- Cookie available across whole website
put cookie "testcookie3" for "/" on "www.livecode.com" with "some cookie value"
-- Cookie available to specific pages
put cookie "testcookie4" for "/products/" on "www.livecode.com" with "some cookie value" until (the seconds + 60 * 60 * 24 * 365)
put secure httponly cookie "testcookie4" for "/products/" on "www.livecode.com" with "some cookie value" until (the seconds + 60 * 60 * 24 * 365)

keyword: $_COOKIE

Compatibility and Support

Introduced

LiveCode 4.6.4

OS

mac

windows

linux

Platforms

server

Thank you for your feedback!

Was this page helpful?